package zeno.project.springboot.controller.core;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import zeno.project.springboot.controller.base.BaseController;
import zeno.project.springboot.entity.User;
import zeno.project.springboot.entity.base.JsonResult;
import zeno.project.springboot.service.UserService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 统一的返回值类型及测试
 * @author zhiyong
 */
@RestController
@RequestMapping("/jsonResult")
@Api(value = "jsonResult返回学习")
public class JsonResultController extends BaseController {
    @Resource
    private UserService userService;

    @GetMapping("/user")
    @ApiOperation(value = "获取用户")
    public JsonResult<User> getUser(@RequestParam(value = "userName") String userName) {
        User user = userService.getUserByName(userName);
        return new JsonResult<>(user);
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取用户列表")
    public JsonResult<Object> getUserList() {
        List<User> userList = new ArrayList<>();
        User user1 = new User(1, "倪升武", "123456");
        User user2 = new User(2, "达人课", "123456");
        userList.add(user1);
        userList.add(user2);
        return new JsonResult<>(userList, "获取用户列表成功");
    }

    @GetMapping("/map")
    @ApiOperation(value = "获取用户Map")
    public JsonResult<Object> getMap() {
        Map<String, Object> map = new HashMap<>(3);
        User user = new User(1, "倪升武", null);
        map.put("作者信息", user);
        map.put("博客地址", "http://blog.itcodai.com");
        map.put("CSDN地址", null);
        map.put("粉丝数量", 4153);
        return new JsonResult<>(map);
    }
}
